package com.rsxy.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rsxy.service.pojo.RsOrder;
import com.rsxy.service.pojo.RsProduct;
import com.rsxy.service.service.RsOrderService;
import com.rsxy.service.service.RsProductService;
import com.rsxy.service.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(tags= "订单相关接口")
public class OrderController {
    @Autowired
    private RsOrderService rsOrderService;
    @Autowired
    private RsProductService rsProductService;
    @ApiOperation(value = "生成订单",notes = "测试数据")
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public String add(RsOrder rsOrder){
        int insert = rsOrderService.getBaseMapper().insert(rsOrder);
     //   log.info("插入:"+insert);
        if (insert>0){
            QueryWrapper<RsProduct> rsProductQueryWrapper=new QueryWrapper<>();
            RsProduct rsProduct=new RsProduct();
            rsProduct.setProductStatus(1);
            rsProductQueryWrapper.eq("product_id",rsOrder.getProductId());
            int update = rsProductService.updateByProductId(rsOrder.getProductId());
           // log.info(""+update);
            if (update>0){
                return ResultUtil.success(200,"下单成功",null);
            }
        }
        return ResultUtil.success(101,"下单失败",null);
    }
    @ApiOperation(value = "获取所有订单",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value="/order")
    public IPage<RsOrder> getPageList(Integer page, Integer limit){
        QueryWrapper<RsOrder> wrapper = new QueryWrapper();
        wrapper.select(RsOrder.class,i -> !i.getColumn().equals("rs_product"));
        Page<RsOrder> page2=new Page<>(page,limit);
        IPage<RsOrder> page1 = rsOrderService.getBaseMapper().selectPage(page2, wrapper);

        return page1;
    }
    @ApiOperation(value = "获取个人所有订单",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value="/order/{userId}/v1")
    public IPage<RsOrder> getPageInfoList(@PathVariable("userId") String userId,Integer page, Integer limit){
        QueryWrapper<RsOrder> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<RsOrder> page2=new Page<>(page,limit);
        IPage<RsOrder> page1 = rsOrderService.getBaseMapper().selectPage(page2, wrapper);
        return page1;
    }
}
